define(["require", "exports","./heatmap"], function (require, exports ,Heatmap) {
	var CsHPConfig = {
		container:null,    //heatmap的渲染容器
		initExtent:null,   //限制热力图显示范围,如果不填写将以点的分布最大范围来设置， 对象结构：{"minx":103.222,"miny":30.3333,"maxx":103.444,"maxy":30.555"}
		radius:100,       //数据缓冲半径
		viewWidth:1000,   //生成canvas的宽度
		viewHeight:1000,   //生成canvas的高度
		blur:0.85,           //模糊因子越高，渐变色梯度越平滑。
		maxOpacity: .5,      //最大值的不透明度
        minOpacity: 0,       //最小值的不透明度，当前设置为透明
        gradient:null,        //渐变色定制,
        points:[],             //坐标点以及值，  结构[{"lon":103.222,"lat":30.22,"value":20},.....]
        basePoint: null,      //当前平面坐标参考基础点, 结构{"lon":103.222,"lat":30.22}
        curExtent:null         //当前热力图实际显示范围，对象结构：{"minx":103.222,"miny":30.3333,"maxx":103.444,"maxy":30.555}
        
	};
    function CsHeatmap(config) {
    	 CsHPConfig.initExtent = config.extent==null?CsHPConfig.extent:config.extent;
    	 CsHPConfig.radius = config.radius==null?CsHPConfig.radius:config.radius; 
    	 CsHPConfig.viewWidth = config.viewWidth==null?CsHPConfig.viewWidth:config.viewWidth; 
    	 CsHPConfig.viewHeight = config.viewHeight==null?CsHPConfig.viewHeight:config.viewHeight; 
    	 CsHPConfig.blur = config.blur==null?CsHPConfig.blur:config.blur;
    	 CsHPConfig.gradient = config.gradient==null?CsHPConfig.gradient:config.gradient; 
    	 CsHPConfig.maxOpacity = config.maxOpacity==null?CsHPConfig.maxOpacity:config.maxOpacity; 
    	 CsHPConfig.minOpacity = config.minOpacity==null?CsHPConfig.minOpacity:config.minOpacity; 
    	 CsHPConfig.points = config.points==null?CsHPConfig.points:config.points; 
    	 this.entity = null; //当前渲染在cesium上的热力图实体
    	 CsHPConfig.container = $("<div style='height:"+CsHPConfig.viewHeight+"px;width:"+CsHPConfig.viewWidth+"px;'></div>")[0];
    	 this.heatmap = ObjectCreater.CreateHeatmap();
    	 this.setData(CsHPConfig.points);
    	 this.repaint();
    }

    
    ///实时添加数据: 参数lon,lat,value 或[{"lon":103.22,"lat":30.44,"value":20},{"lon":103.22,"lat":30.44,"value":20},....]
    CsHeatmap.prototype.addData = function(){
    	if(arguments.length>0)
    	{
    		if(arguments.length==1 && arguments[0] instanceof Array){
    			var pointlist = arguments[0];
    			for(var i in pointlist)
    			{
    				CsHPConfig.points.push({"lon":pointlist[i]["lon"],"lat":pointlist[i]["lat"],"value":pointlist[i]["value"]});
    			}
    		}
    		else if(arguments.length==3)
    		{
    			CsHPConfig.points.push({"lon":arguments[0],"lat":arguments[1],"value":arguments[2]});
    		}
    		var data = Coordinate.LonlatTranToView();
    		var maxvalue = data[0].value;
    		var minvalue = data[0].value;
    		for(var i in data){
    			if(data[i]>maxvalue)
    			maxvalue = data[i];
    			if(data[i]<minvalue)
    			minvalue = data[i];
    		}
    		this.heatmap.setData({max:maxvalue,data:data});
    		this.repaint();
    	}
    };
    
    //批量设置数据  对象结构[{"lon":103.22,"lat":30.44,"value":20},...]
    CsHeatmap.prototype.setData = function(points){
    	var maxvalue = points[0].value;
    	CsHPConfig.points = [];
    	for(var i in points){
    		CsHPConfig.points.push({"lon":points[i].lon,"lat":points[i].lat,"value":points[i].value});
    	}
    	var data = Coordinate.LonlatTranToView();
    	var maxvalue = data[0].value;
    	var minvalue = data[0].value;
    	for(var i in data){
    		if(data[i].value>maxvalue)
    		  maxvalue = data[i].value;
    		if(data[i]<minvalue)
    		  minvalue = data[i].value;
    	}
    	this.heatmap.setData({max:maxvalue,data:data});
    	this.repaint();
    };
    
     //设置最大数据
    CsHeatmap.prototype.setDataMax = function(number){
    	this.heatmap.setDataMax(number);
    };
    
    //设置最小数据
    CsHeatmap.prototype.setDataMin = function(number){
    	this.heatmap.setDataMin(number);
    };
    
    //设置配置信息
    CsHeatmap.prototype.configure = function(Data){
    	this.heatmap.configure(Data);
    };
    
    //获取指定坐标的数值
    CsHeatmap.prototype.getValueAt = function(Data){
    	this.heatmap.getValueAt(Data);
    };
    
    //获取数据
    CsHeatmap.prototype.getData = function(){
    	
    };
    
    //设置数据
    CsHeatmap.prototype.getDataUrl = function(){
    	
    };
    
    //重新绘制热力图
    CsHeatmap.prototype.repaint = function(){
    	this.heatmap.repaint();
    	var canvas = $(CsHPConfig.container).find("canvas")[0];
    	if(this.entity!=null){
    		this.entity.plane.material = new Cesium.ImageMaterialProperty({
    			image:canvas,
    			transparent:true
    		});
    	}
    };
    
    //加载呈现热力图的Cesium的Entity对象
    //entityCollect:Cesium.EntityCollection
    CsHeatmap.prototype.loadEntity = function(entityCollect){
    	if(entityCollect == null || entityCollect instanceof Cesium.EntityCollection)
    	{
    	   throw new Error('entityCollect参数不能为空，且对象必须为Cesium.EntityCollection类型');
    	   return;
    	}
    	var entityobj = entityCollect.add(entityJson);
    	this.entity = entityobj;
    	return this.entity;
    };
    
    //获取热力图实体对象
    CsHeatmap.prototype.getEntity = function(){
    	return this.entity;
    };
    
   
   /*-----------对象构建器-----------------*/
    var ObjectCreater = (function(CsHPConfig){
  	   function ObjectCreater(CsHPConfig){
    	 this.CsHPConfig = CsHPConfig;
       }

        ObjectCreater.prototype.CreateHeatmap=function(){
     		  heatmap = Heatmap.create(this.CsHPConfig);
     		  return heatmap;
     	   };
     	   ObjectCreater.prototype.CreateEntity=function(){
     		
     	   }
        return new ObjectCreater(CsHPConfig);
     })(CsHPConfig);
     
     /*-----------坐标转换器-----------------*/
    var Coordinate = (function(CsHPConfig){
  	   function Coordinate(CsHPConfig){
    	  this.CsHPConfig = CsHPConfig;
       }

       	   //经纬度坐标转视图坐标 参数结构 [{"lon":103.22,"lat":30.11,"value":20},......]
     	   Coordinate.prototype.LonlatTranToView = function(){
     		  var points = this.CsHPConfig.points;
     		  var extent = this.CsHPConfig.initExtent;
     		  if(extent == null)
     		  {
     		    extent = {"minx":points[0].lon,"miny":points[0].lat,"maxx":points[0].lon,"maxy":points[0].lat};
     		    for(var i=0;i<points.length;i++){
     		  	  var lon = points[i].lon;
     		  	  var lat = points[i].lat;
     		  	  var value = points[i].value;
     		  	  if(lon < extent.minx)
     		  	   extent.minx = lon;
     		  	  if(lon > extent.maxx)
     		  	   extent.maxx = lon;
     		  	  if(lat < extent.miny)
     		  	   extent.miny = lat;
     		  	  if(lat > extent.maxy)
     		  	   extent.maxy = lat;
     		    }
     		  }
     		  this.CsHPConfig.curExtent = extent;
     		  this.CsHPConfig.basePoint = {"lon":extent.minx,"lat":extent.maxy};
     		  var exPoints = [];
     		  for(var i=0; i<points.length;i++)
     		  {
     		  	 var width = this.LonlatDistance(extent.minx,extent.miny,extent.maxx,extent.miny);  //最大范围宽度
     		  	 var height = this.LonlatDistance(extent.minx,extent.miny,extent.minx,extent.maxy);  //最大范围高度
     		  	 var viewWidth = this.CsHPConfig.viewWidth; //视图范围宽度
     		  	 var viewHeight = this.CsHPConfig.viewHeight; //视图范围高度
     		  	 var xy = this.LonlatXYDistance(this.CsHPConfig.basePoint.lon,this.CsHPConfig.basePoint.lat,points[i].lon,points[i].lat);
     		  	 var point = {"x":xy["x"]/width*viewWidth,"y":xy["y"]/height*viewHeight,"value": points[i].value};
     		  	 exPoints.push(point);
     		  }
     		  
     		  return exPoints;
     		  
     	   };
     	   //根据经纬度获取视图坐标
     	   Coordinate.prototype.getViewByLonlat=function(){
     		
     	   };
     	   //获取经纬度坐标点横纵坐标距离 返回值{x:1,y:2}
     	   Coordinate.prototype.LonlatXYDistance = function(lon1,lat1,lon2,lat2){
     	   	   var left = Cesium.Cartesian3.fromDegrees(lon1,lat1,0);
     	   	   var right = Cesium.Cartesian3.fromDegrees(lon2,lat2,0);
     	   	   var distance = Cesium.Cartesian3.distance(left, right);
     	   	   var angle = Cesium.Cartesian3.angleBetween(left,right);
     	   	   var x = distance*Math.cos(angle);
     	   	   var y = distance*Math.sin(angle);
     	   	   return {"x":x,"y":y};
     	   }
     	   
     	   //获取经纬度坐标点直线坐标距离 
     	   Coordinate.prototype.LonlatDistance = function(lon1,lat1,lon2,lat2){
     	   	   var left = Cesium.Cartesian3.fromDegrees(lon1,lat1,0);
     	   	   var right = Cesium.Cartesian3.fromDegrees(lon2,lat2,0);
     	   	   var distance = Cesium.Cartesian3.distance(left, right);
     	   	   return distance;
     	   }
    
        return new Coordinate(CsHPConfig);
     })(CsHPConfig);
  
      return CsHeatmap;
});
